package com.hnhy.bluetooth.base

import android.Manifest
import android.annotation.SuppressLint
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.LocationManager
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.widget.Toast
import android.widget.Toolbar
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity

open class BaseActivity : AppCompatActivity() {

    private var context: Context? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        context = this
    }

    /**
     * 跳转页面
     * @param clazz
     * @param finish 默认false不销毁当前Activity
     */
    protected fun jumpActivity(clazz: Class<*>?, finish: Boolean = false) {
        startActivity(Intent(context, clazz))
        if (finish) finish()
    }

    /**
     * ToolBar标题栏左边的操作按钮
     * @param toolbar
     * @param finish 默认false不销毁当前Activity
     */
    protected fun back(toolbar: Toolbar, finish: Boolean = false) =
        toolbar.setNavigationOnClickListener { if (finish) finish() else onBackPressed() }

    /**
     * Toast提示
     * @param msg 提示文本
     */
    protected fun showMsg(msg: CharSequence) =
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()

    /**
     * 判断是否为 Android 12
     */
    protected open fun isAndroid12() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S

    protected open fun hasAccessFineLocation() =
        hasPermission(Manifest.permission.ACCESS_FINE_LOCATION)

    protected open fun hasCoarseLocation() =
        hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION)

    @RequiresApi(Build.VERSION_CODES.S)
    protected open fun hasBluetoothConnect() = hasPermission(Manifest.permission.BLUETOOTH_CONNECT)

    @RequiresApi(Build.VERSION_CODES.S)
    protected open fun hasBluetoothScan() = hasPermission(Manifest.permission.BLUETOOTH_SCAN)

    /**
     * 检查是有拥有某权限
     *
     * @param permission 权限名称
     * @return true 有  false 没有
     */
    @SuppressLint("NewApi")
    protected open fun hasPermission(permission: String) = checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED

    /**
     * 蓝牙是否打开
     *
     * @return true or false
     */
    protected open fun isOpenBluetooth(): Boolean {
        (getSystemService(BLUETOOTH_SERVICE) as BluetoothManager).also {
            it.adapter ?: return false
            return it.adapter.isEnabled
        }
    }

    /**
     * 位置是否打开
     */
    protected open fun isOpenLocation(): Boolean {
        val locationManager = getSystemService(LOCATION_SERVICE) as LocationManager
        val gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
        val network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
        val locationEnabled = isLocationEnabled()
        Log.e("TAG", "gps: $gps，network：$network，locationEnabled：$locationEnabled")
        return gps || network || locationEnabled
    }

    open fun isLocationEnabled(): Boolean {
        val locationMode = try {
            Settings.Secure.getInt(contentResolver, Settings.Secure.LOCATION_MODE)
        } catch (e: Settings.SettingNotFoundException) {
            e.printStackTrace()
            return false
        }
        return locationMode != Settings.Secure.LOCATION_MODE_OFF
    }
}
